/**
 * Personal Digital Assistance
 * 
 * 2010-9-14
 * 
 */

/**
 * @author Kevin Hanson
 *
 */
package pda;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import note.NotePanel;
import account.AccountPanel;
import database.Database;
import diary.DiaryPanel;

public class PDA extends JPanel {
	private static final long serialVersionUID = 1L;
	static String[] tabNames = { "Calendar", "Note", "Diary", "Log", "Finance", "Account" };
	final int WIDTH = 800, HEIGHT = 600;
	private Database db = null;

	public PDA() {
		super();
	}

	private JMenuBar createMenuBar() {
		// JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		JMenuBar menuBar = new JMenuBar();
		// File menu items
		JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
		// Element menu items
		JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem, textItem;
		JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;

		JMenu fileMenu = new JMenu("File");
		JMenu elementMenu = new JMenu("Elements");
		fileMenu.setMnemonic('F');
		elementMenu.setMnemonic('E');

		// Construct the file pull down menu
		newItem = fileMenu.add("New");
		openItem = fileMenu.add("Open");
		closeItem = fileMenu.add("Close");
		fileMenu.addSeparator();
		saveItem = fileMenu.add("Save");
		saveAsItem = fileMenu.add("Save As...");

		JMenu colorMenu = new JMenu("Color");

		fileMenu.addSeparator();
		printItem = fileMenu.add("Print");

		newItem.setAccelerator(KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
		openItem.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
		saveItem.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
		printItem.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

		elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
		elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
		elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
		elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
		ButtonGroup types = new ButtonGroup();
		types.add(lineItem);
		types.add(rectangleItem);
		types.add(circleItem);
		types.add(curveItem);

		lineItem.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
		rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
		circleItem.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
		curveItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));

		elementMenu.addSeparator();

		elementMenu.add(colorMenu); // Add the sub-menu
		colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
		colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
		colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
		colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));

		// Add element color accelerators
		redItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
		yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', Event.CTRL_MASK));
		greenItem.setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
		blueItem.setAccelerator(KeyStroke.getKeyStroke('B', Event.CTRL_MASK));

		menuBar.add(fileMenu);
		menuBar.add(elementMenu);

		return menuBar;
	}

	private JTabbedPane createTabbedPane() {
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setTabPlacement(JTabbedPane.LEFT);

		tabbedPane.addTab(tabNames[1], new NotePanel());
		tabbedPane.addTab(tabNames[2], new DiaryPanel());
		tabbedPane.addTab(tabNames[3], new JCheckBox());
		tabbedPane.addTab(tabNames[4], new JCheckBox());
		tabbedPane.addTab(tabNames[5], new AccountPanel());
		
		tabbedPane.setSelectedIndex(1);

		return tabbedPane;
	}

	public void createAndShowGUI() {
		try {
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
		}
		JFrame.setDefaultLookAndFeelDecorated(false);

		JFrame frame = new JFrame();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((d.width - WIDTH) / 2, (d.height - HEIGHT) / 2);
		frame.setSize(WIDTH, HEIGHT);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setJMenuBar(createMenuBar());

		setLayout(new BorderLayout());
		add(createTabbedPane(), BorderLayout.CENTER);

		frame.getContentPane().add(this);
		frame.setVisible(true);
	}

	void init() {
		db = new Database();
		createAndShowGUI();
	}

	public static void main(String[] args) {
		final PDA pda = new PDA();

		// Schedule a job for event-dispatching thread:
		// creating and showing this application's GUI
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				pda.init();
			}
		});
	}
}